package cn.bluemobi.platform.controller.order;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bluemobi.common.core.dto.MapDto;
import cn.bluemobi.common.core.dto.PageCondition;
import cn.bluemobi.platform.core.controller.PlatformBaseController;
import cn.bluemobi.platform.core.dto.DatatablePage;
import cn.bluemobi.platform.entity.order.Order;
import cn.bluemobi.platform.service.OrderService;

/**
 * 
 * @author lvxh
 * 
 */
@Controller
@RequestMapping("/admin/order")
public class OrderController extends PlatformBaseController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("/toOrderPage")
    public String toOrderPage(Model model) {
        return "order/orderList";
    }

    @RequestMapping("/findOrders")
    @ResponseBody
    public DatatablePage findOrders(PageCondition cond) {
        return new DatatablePage(cond, orderService.findOrders(cond, getUser()));
    }

    @RequestMapping("/toOrderDetailPage")
    public String toOrderDetailPage(@RequestParam(value = "orderId", required = false) String orderId, ModelMap model) {
        model.put("logisticsList", orderService.findLogisticsList());
        model.put("address", orderService.findOrderAddressDetail(orderId));
        if (!StringUtils.isEmpty(orderId)) {
            Order order = orderService.findOrderDetail(orderId);
            model.put("order", order);
        }
        return "order/orderDetail";
    }

    @RequestMapping("/confirmDelivery")
    @ResponseBody
    public MapDto confirmDelivery(@RequestParam("orderId") String orderId,
            @RequestParam("logisticsCode") String logisticsCode, @RequestParam("logisticsNumber") String logisticsNumber) {
        return orderService.confirmDelivery(orderId, logisticsCode, logisticsNumber, getUser());
    }

    @RequestMapping("/deleteOrder")
    @ResponseBody
    public MapDto deleteById(@RequestParam("orderId") String orderId) {
        return orderService.deleteOrder(orderId, getUser());
    }

    @RequestMapping("/sendReward")
    @ResponseBody
    public MapDto sendReward(@RequestParam("orderId") String orderId, @RequestParam("memberId") String memberId) {
        return orderService.sendReward(orderId, memberId, getUser());
    }

}
